<?php
namespace services\payment;

use think\Request;
use Carbon\Carbon;
use think\Config;
use app\common\exception\OrderException;
use services\PaymentInterface;
use think\Db;

/**
 * 余额支付
 */
class Balance implements PaymentInterface
{
    /**
     * 统一下单
     * @param  [type] $attributes [description]
     * @return [type]               [description]
     */
    public function pay($attributes)
    {
        $result = ['code' => 200, 'data' => 'SUCCESS!'];
        $order  = json_decode($attributes['detail']);

        if ($attributes['attach'] == 'order') {
            $_order = model('Orders')->getOrderInfo(['number' => $attributes['out_trade_no']]);
        }elseif ($attributes['attach'] == 'recharge') {
            $_order = model('Intergrateadds')->getOrderInfo(['order_num' => $attributes['out_trade_no']]);
        }
        if ($_order) {
            if ($_order->paid_at) {
                $result = ['code' => 201, 'data' => 'Repeated'];
            }else{
                $rent_user_id = $order->rent_user_id;//订单用户id
                $order_amount = $order->total_amount;//订单金额
                Db::startTrans();
                try {
                    $this->completeOrderPay($order->number);
                    $this->reduceCapital($rent_user_id, $order_amount);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $result = ['code' => -100, 'data' => 'Order not exist.'];
                }
                $result['data'] = $attributes['notify_url'];
                return $result;
            }
        }else{
            $result = ['code' => -101, 'data' => 'Order not exist.'];
        }
        throw new OrderException($result['code'], $result['data']); 
    }
    /**
     * 查询订单
     * @param  [type] $order_no [description]
     * @param  string $type     [description]
     * @return [type]           [description]
     */
    public function queryOrder($order_no, $type = 'out_trade_no')
    {
        $_order = model('Orders')->getOrderInfo(['number' => $order_no]) ? : model('Intergrateadds')->getOrderInfo(['order_num' => $order_no]);
        return $_order;
    }
    /**
     * 撤销订单
     * @param  [type] $order_no 订单号
     * @param  string $type     out_trade_no 内部订单号 | transaction_id 微信订单号
     * @return [type]           [description]
     */
    public function reverseOrder($order_no, $type = 'out_trade_no')
    {

    }
    /**
     * 关闭订单
     * @param  [type] $order_no [description]
     * @return [type]           [description]
     */
    public function closeOrder($order_no)
    {

    }
    /**
     * 下载对账单
     * @param  [type] $date [description]
     * @param  string $type [description]
     * ALL：返回当日所有订单信息（默认值）
     * SUCCESS：返回当日成功支付的订单
     * REFUND：返回当日退款订单
     * REVOKED：已撤销的订单
     * @return [type]       [description]
     */     
    public function downloadBill($date, $type = 'ALL')
    {
    }
    /**
     * 退款
     * @param  [type] $out_trade_no [description]
     * @param  [type] $total_fee    [description]
     * @param  [type] $refund_fee   [description]
     * @return [type]               [description]
     */
    public function refund($out_trade_no, $total_fee, $refund_fee)
    {
        return ;
    }
    /**
     * 支付结果通知
     * @return [type] [description]
     */
    public function notify()
    {
        
    }
    /**
     * 同步扣除资金
     * @param  [type] $user_id [description]
     * @param  [type] $amount  [description]
     * @return [type]          [description]
     */
    private function reduceCapital($user_id, $amount)
    {
        model('Capitals')->reduceCapital($amount, $user_id, ['type' => '消费', 'remark' => '余额支付'], true);
    }
    private function completeOrderPay($ordeer_id)
    {
        model('Orders')->completeOrderPay($ordeer_id);
    }
}
